﻿Imports System.Data
Imports System.Collections.ObjectModel
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

#Region "champs"
    'Liste des données
    Private listSitesWeb As New Collection(Of dataReportSites)
    Private listServeurs As New Collection(Of dataReportServeurs)
    Private listWebServices As New Collection(Of dataReportServices)

    'Statut
    Private bReverificationSites As Boolean = False
    Private bErreurSites As Boolean = False
    Private bReverificationServeurs As Boolean = False
    Private bErreurServeurs As Boolean = False
    Private bReverificationServices As Boolean = False
    Private bErreurServices As Boolean = False

    'Variables des données (SGBD)
    Private sConnexion As String = "Data Source=POSTE-DEV16;Initial Catalog=KelControl;User ID=sa;Password=kelstage"
    Private sqlConnexion As SqlConnection
    Private scConnexion As SqlCommand
    Private dataConnexion As New SqlDataAdapter
    Private dataReadConnexion As SqlDataReader
    Private dsConnexion As New DataSet
#End Region

#Region "Methodes"
    Private Sub recupererStatutSites()
        Dim sRequete = "SELECT url, date, statut, message, nbErreur FROM Rapport_sites " & _
                       "WHERE idPassage = (SELECT MAX(idPassage) FROM Rapport_sites)"
        Dim row As Object() = Nothing
        listSitesWeb.Clear()

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        Dim dataSite As dataReportSites

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            dataSite.uSiteUrl = New Uri(Trim(row(0).ToString))
            dataSite.dVerificationDate = Trim(row(1).ToString)
            Select Case Trim(row(2).ToString)
                Case "Aucune"
                    dataSite.sVerificationStatut = enStatutVerifications.Aucune
                Case "Erreur"
                    dataSite.sVerificationStatut = enStatutVerifications.Erreur
                    bErreurSites = True
                Case "ErreurMajeur"
                    dataSite.sVerificationStatut = enStatutVerifications.ErreurMajeur
                    bErreurSites = True
                Case "ErreurMineur"
                    dataSite.sVerificationStatut = enStatutVerifications.ErreurMineur
                    bErreurSites = True
                Case "ReverificationRequise"
                    dataSite.sVerificationStatut = enStatutVerifications.ReverificationRequise
                    bReverificationSites = True
            End Select
            dataSite.sMessageErreur = Trim(row(3).ToString)
            dataSite.iErreurCount = row(4)
            listSitesWeb.Add(dataSite)
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub recupererStatutServeurs()
        Dim sRequete = "SELECT ip, date, statut, message, nbErreur FROM Rapport_serveurs " & _
                       "WHERE idPassage = (SELECT MAX(idPassage) FROM Rapport_serveurs)"
        Dim row As Object() = Nothing
        listServeurs.Clear()

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        Dim dataServeur As dataReportServeurs

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            dataServeur.uServeurIP = System.Net.IPAddress.Parse(Trim(row(0).ToString))
            dataServeur.dVerificationDate = Trim(row(1).ToString)
            Select Case Trim(row(2).ToString)
                Case "Aucune"
                    dataServeur.sVerificationStatut = enStatutVerifications.Aucune
                Case "Erreur"
                    dataServeur.sVerificationStatut = enStatutVerifications.Erreur
                    bErreurServeurs = True
                Case "ErreurMajeur"
                    dataServeur.sVerificationStatut = enStatutVerifications.ErreurMajeur
                    bErreurServeurs = True
                Case "ErreurMineur"
                    dataServeur.sVerificationStatut = enStatutVerifications.ErreurMineur
                    bErreurServeurs = True
                Case "ReverificationRequise"
                    dataServeur.sVerificationStatut = enStatutVerifications.ReverificationRequise
                    bReverificationServeurs = True
            End Select
            dataServeur.sMessageErreur = Trim(row(3).ToString)
            dataServeur.iErreurCount = row(4)
            listServeurs.Add(dataServeur)
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub recupererStatutServices()
        Dim sRequete = "SELECT url, methode, date, statut, message, nbErreur FROM Rapport_services " & _
                       "WHERE idPassage = (SELECT MAX(idPassage) FROM Rapport_services)"
        Dim row As Object() = Nothing
        listWebServices.Clear()

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        Dim dataService As dataReportServices

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            dataService.uServiceUrl = New Uri(Trim(row(0).ToString))
            dataService.uServiceName = String.Empty
            dataService.uServiceMethode = Trim(row(1).ToString)
            dataService.dVerificationDate = Trim(row(2).ToString)
            Select Case Trim(row(3).ToString)
                Case "Aucune"
                    dataService.sVerificationStatut = enStatutVerifications.Aucune
                Case "Erreur"
                    dataService.sVerificationStatut = enStatutVerifications.Erreur
                    bErreurServices = True
                Case "ErreurMajeur"
                    dataService.sVerificationStatut = enStatutVerifications.ErreurMajeur
                    bErreurServices = True
                Case "ErreurMineur"
                    dataService.sVerificationStatut = enStatutVerifications.ErreurMineur
                    bErreurServices = True
                Case "ReverificationRequise"
                    dataService.sVerificationStatut = enStatutVerifications.ReverificationRequise
                    bReverificationServices = True
            End Select
            dataService.sMessageErreur = Trim(row(4).ToString)
            dataService.iErreurCount = row(5)
            listWebServices.Add(dataService)
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub afficherRapportSites()
        If (bErreurSites = True) Then
            etatSites.Attributes.Add("class", "etatErreur")
        ElseIf (bReverificationSites = True) Then
            etatSites.Attributes.Add("class", "etatReverification")
        Else
            etatSites.Attributes.Add("class", "etatOk")
        End If

        liRapportSite.InnerHtml = ""
        Dim listContent As String
        For i = 0 To listSitesWeb.Count - 1 Step 1
            If (listSitesWeb(i).sVerificationStatut = enStatutVerifications.Aucune) Then
                listContent = "<li class='etatOk'>"
                listContent &= "<div class='nom'>" & listSitesWeb(i).uSiteUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listSitesWeb(i).dVerificationDate.ToString & "]"
                listContent &= "</div>"
            ElseIf (listSitesWeb(i).sVerificationStatut = enStatutVerifications.ReverificationRequise) Then
                listContent = "<li class='etatReverification'>"
                listContent &= "<div class='nom'>" & listSitesWeb(i).uSiteUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listSitesWeb(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listSitesWeb(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            Else
                listContent = "<li class='etatErreur'>"
                listContent &= "<div class='nom'>" & listSitesWeb(i).uSiteUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listSitesWeb(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Récursivité de l'erreur : <strong>" & listSitesWeb(i).iErreurCount.ToString & "</strong>"
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listSitesWeb(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            End If
            liRapportSite.InnerHtml &= listContent
        Next
    End Sub

    Private Sub afficherRapportServeurs()
        If (bErreurServeurs = True) Then
            Me.etatServeurs.Attributes.Add("class", "etatErreur")
        ElseIf (bReverificationServeurs = True) Then
            Me.etatServeurs.Attributes.Add("class", "etatReverification")
        Else
            Me.etatServeurs.Attributes.Add("class", "etatOk")
        End If

        liRapportServeurs.InnerHtml = ""
        Dim listContent As String
        For i = 0 To listServeurs.Count - 1 Step 1
            If (listServeurs(i).sVerificationStatut = enStatutVerifications.Aucune) Then
                listContent = "<li class='etatOk'>"
                listContent &= "<div class='nom'>" & listServeurs(i).uServeurIP.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listServeurs(i).dVerificationDate.ToString & "]"
                listContent &= "</div>"
            ElseIf (listServeurs(i).sVerificationStatut = enStatutVerifications.ReverificationRequise) Then
                listContent = "<li class='etatReverification'>"
                listContent &= "<div class='nom'>" & listServeurs(i).uServeurIP.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listServeurs(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listServeurs(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            Else
                listContent = "<li class='etatErreur'>"
                listContent &= "<div class='nom'>" & listServeurs(i).uServeurIP.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos' >"
                listContent &= "    &bull; Date de vérification : [" & listServeurs(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Récursivité de l'erreur : <strong>" & listServeurs(i).iErreurCount.ToString & "</strong>"
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listServeurs(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            End If
            liRapportServeurs.InnerHtml &= listContent
        Next
    End Sub

    Private Sub afficherRapportServices()
        If (bErreurServices = True) Then
            Me.etatServices.Attributes.Add("class", "etatErreur")
        ElseIf (bReverificationServices = True) Then
            Me.etatServices.Attributes.Add("class", "etatReverification")
        Else
            Me.etatServices.Attributes.Add("class", "etatOk")
        End If

        liRapportServices.InnerHtml = ""
        Dim listContent As String
        For i = 0 To listWebServices.Count - 1 Step 1
            If (listWebServices(i).sVerificationStatut = enStatutVerifications.Aucune) Then
                listContent = "<li class='etatOk'>"
                listContent &= "<div class='nom'>" & listWebServices(i).uServiceUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos'>"
                listContent &= "    &bull; Date de vérification : [" & listWebServices(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Méthode appelée : " & listWebServices(i).uServiceMethode.ToString
                listContent &= "</div>"
            ElseIf (listWebServices(i).sVerificationStatut = enStatutVerifications.ReverificationRequise) Then
                listContent = "<li class='etatReverification'>"
                listContent &= "<div class='nom'>" & listWebServices(i).uServiceUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos'>"
                listContent &= "    &bull; Date de vérification : [" & listWebServices(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Méthode appelée : " & listWebServices(i).uServiceMethode.ToString
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listWebServices(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            Else
                listContent = "<li class='etatErreur'>"
                listContent &= "<div class='nom'>" & listWebServices(i).uServiceUrl.ToString & "</div>"
                listContent &= "<div class='btnPlus'>[-]</div>"
                listContent &= "<div class='clear'></div>"
                listContent &= "<div class='areaInfos'>"
                listContent &= "    &bull; Date de vérification : [" & listWebServices(i).dVerificationDate.ToString & "]"
                listContent &= "<br />&bull; Méthode appelée : " & listWebServices(i).uServiceMethode.ToString
                listContent &= "<br />&bull; Récursivité de l'erreur : <strong>" & listWebServices(i).iErreurCount.ToString & "</strong>"
                listContent &= "<br />&bull; Message d'erreur : <strong>" & listWebServices(i).sMessageErreur.ToString & "</strong>"
                listContent &= "</div>"
            End If
            liRapportServices.InnerHtml &= listContent
        Next
    End Sub
#End Region

#Region "Events"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        sqlConnexion = New SqlConnection()
        sqlConnexion.ConnectionString = sConnexion
        sqlConnexion.Open()
        recupererStatutSites()
        recupererStatutServeurs()
        recupererStatutServices()
        sqlConnexion.Dispose()
        sqlConnexion.Close()

        afficherRapportSites()
        afficherRapportServeurs()
        afficherRapportServices()
    End Sub
#End Region

End Class
